﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.Base;
using ZBJF.Warranty.Common.Enums.InsuranceWarranty;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;
using ZBJF.Warranty.Model.ViewModel.InsuranceProducts;
namespace ZBJF.Warranty.BLL
{
    public partial class InsuranceProductsAuditProjectConfigurationMappingsService
    {
        public IQueryable<InsuranceProductsAuditProjectConfigurationMappings> LoadByInsuranceProductID(
            long insuranceProductID)
        {
            return LoadEntities(o => o.CellPhoneServiceProjectID == insuranceProductID);
        }

        public bool IsExist(long insuranceProductID, int auditProjectConfigurationID)
        {
            return LoadEntities(o => o.CellPhoneServiceProjectID == insuranceProductID && o.AuditProjectConfigurationID == auditProjectConfigurationID).Any();
        }

        public List<InsuranceProductAuditProjectConfigurationSnapshoot> GetAuditProjectConfigurationList(
            List<int> cellPhoneServiceProjectIDs, ServiceProductType serviceProjectType, byte auditProjectWithType = 0)
        {
            var insuranceProductAuditProjectConfigurationSnapshoots = new List<InsuranceProductAuditProjectConfigurationSnapshoot>();

            var predicate = PredicateExtensions.True<InsuranceProductsAuditProjectConfigurationMappings>().And(o =>
                cellPhoneServiceProjectIDs.Contains(o.CellPhoneServiceProjectID));
            if (auditProjectWithType > 0)
                predicate = predicate.And(o =>
                    o.AuditProjectConfiguration.AuditProjectWithType == auditProjectWithType);
            var insuranceProductsAuditProjectConfigurationMappings = LoadEntities(predicate).OrderBy(o => o.AuditProjectConfiguration.FieldType).AsNoTracking().ToList();
            if (insuranceProductsAuditProjectConfigurationMappings.Any())
            {
                for (int i = 0; i < insuranceProductsAuditProjectConfigurationMappings.Count; i++)
                {
                    var insuranceProductsAuditProjectConfigurationMapping =
                        insuranceProductsAuditProjectConfigurationMappings[i];
                    if (insuranceProductsAuditProjectConfigurationMapping.AuditProjectConfiguration != null)
                    {
                        if (insuranceProductsAuditProjectConfigurationMapping.AuditProjectConfiguration.FieldType ==
                            (byte)FieldTypeEnum.Text)
                        {
                            insuranceProductAuditProjectConfigurationSnapshoots.Add(new InsuranceProductAuditProjectConfigurationSnapshoot
                                {
                                    InsuranceOrderServiceProjectId = insuranceProductsAuditProjectConfigurationMapping.CellPhoneServiceProjectID,
                                    ServiceProjectType = (byte)serviceProjectType,
                                    AuditProjectWithType = insuranceProductsAuditProjectConfigurationMapping.AuditProjectConfiguration.AuditProjectWithType,
                                    AuditProjectID =
                                        insuranceProductsAuditProjectConfigurationMapping.AuditProjectConfiguration.ID,
                                    AuditProjectName =
                                        insuranceProductsAuditProjectConfigurationMapping.AuditProjectConfiguration.ProjectName,
                                    FieldType =
                                        insuranceProductsAuditProjectConfigurationMapping.AuditProjectConfiguration.FieldType,
                                    OptionID = 0,
                                    OptionName = ""
                                });
                        }
                        else
                        {
                            if (insuranceProductsAuditProjectConfigurationMapping.AuditProjectConfiguration.AuditProjectFieldOption.Any())
                            {
                                var fieldOptions =
                           insuranceProductsAuditProjectConfigurationMapping.AuditProjectConfiguration
                               .AuditProjectFieldOption.ToList();
                                insuranceProductAuditProjectConfigurationSnapshoots.AddRange(fieldOptions.Select(t => new InsuranceProductAuditProjectConfigurationSnapshoot()
                                {
                                    InsuranceOrderServiceProjectId = insuranceProductsAuditProjectConfigurationMapping.CellPhoneServiceProjectID,
                                    ServiceProjectType = (byte)serviceProjectType,
                                    AuditProjectWithType = insuranceProductsAuditProjectConfigurationMapping.AuditProjectConfiguration.AuditProjectWithType,
                                    AuditProjectID = insuranceProductsAuditProjectConfigurationMapping.AuditProjectConfiguration.ID,
                                    AuditProjectName = insuranceProductsAuditProjectConfigurationMapping.AuditProjectConfiguration.ProjectName,
                                    FieldType = insuranceProductsAuditProjectConfigurationMapping.AuditProjectConfiguration.FieldType,
                                    OptionID = t.ID,
                                    OptionName = t.OptionName
                                }));
                            }
                        }
                    }
                }

            }
            return insuranceProductAuditProjectConfigurationSnapshoots.OrderBy(o => o.AuditProjectWithType).ToList();
        }
    }
}
